<template>
  <div class="engineer">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="">
        <el-select v-model="params.customer_id" placeholder="请选择查询的客户">
          <el-option
            :label="item.realname"
            :value="item.id"
            v-for="(item, index) in customer"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-select v-model="params.charge_id" placeholder="请选择查询的主管">
          <el-option
            :label="item.realname"
            :value="item.id"
            v-for="(item, index) in charges"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="toSubmit">查询</el-button>
      </el-form-item>
      <el-button
        type="primary"
        size="small"
        @click="toAddEngineer"
        style="float: right"
        >添加</el-button
      >
    </el-form>
    <div class="table">
      <el-table :data="resData.list" stripe style="width: 100%" :inline="true">
        <el-table-column label="" width="50" type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="工程ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
              <el-form-item label="工程编号">
                <span>{{ props.row.serial_number }}</span>
              </el-form-item>
              <el-form-item label="工程名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="工程类型">
                <span>{{ props.row.type }}</span>
              </el-form-item>
              <el-form-item label="区域经理">
                <span>{{ props.row.charge_name }}</span>
              </el-form-item>
              <el-form-item label="客户姓名">
                <span>{{ props.row.customer_name }}</span>
              </el-form-item>
              <el-form-item label="客户手机号">
                <span>{{ props.row.customer_telephone }}</span>
              </el-form-item>
              <el-form-item label="工程状态">
                <span>{{ props.row.status }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column prop="serial_number" label="工程编号" width="100">
        </el-table-column>
        <el-table-column prop="name" label="工程名称" align="center">
        </el-table-column>
        <el-table-column prop="type" label="工程类型" align="center">
        </el-table-column>
        <el-table-column prop="charge_name" label="区域经理" align="center">
        </el-table-column>
        <el-table-column prop="customer_name" label="客户姓名" align="center">
        </el-table-column>
        <el-table-column
          prop="customer_telephone"
          label="客户手机号"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="status" label="工程状态" align="center">
          <template v-slot="scope">
            <el-tag
              type="succees"
              v-if="scope.row.status === '监测中'"
              size="mini"
              >监测中</el-tag
            >
            <el-tag
              type="danger"
              v-if="scope.row.status === '未绑定'"
              size="mini"
              >未绑定</el-tag
            >
            <el-tag type="info" v-if="scope.row.status === '已结束'" size="mini"
              >已结束</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template v-slot="{ row }">
            <el-button
              type="text"
              style="color: orange"
              @click="toEditEngineer(row)"
              >编辑</el-button
            >
            <el-button
              type="text"
              :disabled="row.status === '已结束' ? true : false"
              @click="toBindEngineer(row)"
              >绑定</el-button
            >
            <el-button
              type="text"
              @click="toEndEngineer(row.id)"
              :disabled="
                row.status === '已结束' || row.status === '未绑定'
                  ? true
                  : false
              "
              >结束</el-button
            >
            <el-button
              type="text"
              @click="toRemoveEngineer(row.id)"
              :disabled="row.status === '监测中' ? true : false"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="parseInt(resData.page)"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="parseInt(resData.pageSize)"
        layout="total, sizes, prev, pager, next, jumper"
        :total="parseInt(resData.total)"
      >
      </el-pagination>
    </div>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="closeDialog"
    >
      <el-form :model="form" :rules="rules" ref="ruleForm" :inline="true">
        <el-form-item
          label="工程编号"
          :label-width="formLabelWidth"
          prop="serial_number"
        >
          <el-input v-model="form.serial_number"></el-input>
        </el-form-item>
        <el-form-item
          label="工程名称"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item
          label="工程地址"
          :label-width="formLabelWidth"
          prop="address"
        >
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item
          label="工程类型"
          :label-width="formLabelWidth"
          prop="type"
        >
          <el-select v-model="form.type">
            <el-option
              :value="item"
              v-for="(item, index) in types"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="工程纬度"
          :label-width="formLabelWidth"
          prop="latitude"
        >
          <el-input v-model="form.latitude"></el-input>
        </el-form-item>
        <el-form-item
          label="工程经度"
          :label-width="formLabelWidth"
          prop="longitude"
        >
          <el-input v-model="form.longitude"></el-input>
        </el-form-item>
        <el-form-item
          label="客户姓名"
          :label-width="formLabelWidth"
          prop="customer_name"
          >{{ form }}
          <el-input
            v-model="form.customer_name"
            :disabled="form.id ? true : false"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="客户电话"
          :label-width="formLabelWidth"
          prop="customer_telephone"
        >
          <el-input
            v-model="form.customer_telephone"
            :disabled="form.id ? true : false"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="主管姓名"
          :label-width="formLabelWidth"
          prop="charge_id"
        >
          <el-select v-model="form.charge_id" placeholder="请选择主管">
            <el-option
              :label="item.realname"
              :value="item.id"
              v-for="(item, index) in charges"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { get, postJSON, del } from "@/utils/request.js";
import _ from "lodash";

export default {
  data() {
    return {
      resData: {},
      params: {
        page: 1,
        pageSize: 10,
      },
      dialogFormVisible: false,
      form: {
        id: "",
        serial_number: "",
        name: "",
        address: "",
        type: "",
        latitude: "",
        longitude: "",
        charge_id: "",
        customer_name: "",
        customer_telephone: "",
      },
      rules: {
        name: [{ required: true, message: "请输入设备名称", trigger: "blur" }],
        serial_number: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
        ],
        address: [{ required: true, message: "请输入地址", trigger: "blur" }],
        type: [
          {
            required: true,
            message: "请输入工程类型",
            trigger: ["change"],
          },
        ],
        latitude: [{ required: true, message: "请输入纬度", trigger: "blur" }],
        longitude: [{ required: true, message: "请输入经度", trigger: "blur" }],
        charge_id: [
          {
            required: true,
            message: "请选择主管",
            trigger: ["change"],
          },
        ],
        customer_name: [
          { required: true, message: "请输入客户姓名", trigger: "blur" },
        ],
        customer_telephone: [
          { required: true, message: "请输入主管电话", trigger: "blur" },
          { min: 11, max: 11, message: "手机号格式不正确", trigger: "blur" },
        ],
      },
      title: "",
      formLabelWidth: "120px",
      charges: [],
      formInline: {
        user: "",
        region: "",
      },
      customer: [],
      types: ["学校", "园区", "写字楼", "建筑工地", "景区", "商场", "医院"],
    };
  },
  created() {
    this.pageQueryEngineer();
    this.getCharge();
    this.getCustomer();
  },
  methods: {
    //事件监听
    //事件监听
    handleSizeChange(pageSize) {
      console.log(pageSize);
      this.params.pageSize = pageSize;
      this.pageQueryEngineer();
    },
    //page改变事件
    handleCurrentChange(page) {
      this.params.page = page;
      this.pageQueryEngineer();
    },
    toAddEngineer() {
      this.title = "添加工程";
      this.form = {};
      this.dialogFormVisible = true;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.$refs["ruleForm"].resetFields();
    },
    submitForm() {
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          postJSON("/engineer/saveOrUpdate", {
            id: this.form.id,
            serial_number: this.form.serial_number,
            name: this.form.name,
            address: this.form.address,
            type: this.form.type,
            latitude: this.form.latitude,
            longitude: this.form.longitude,
            charge_id: parseInt(this.form.charge_id),
            customer_name: this.form.customer_name,
            customer_telephone: this.form.customer_telephone,
          }).then((res) => {
            this.pageQueryEngineer();
            this.$message({
              message: "操作成功",
              type: "success",
            });
            // this.dialogFormVisible = false;
          });
        } else {
          this.$message({
            message: "表单验证失败",
            type: "error",
          });
        }
      });
    },
    toSubmit() {
      this.pageQueryEngineer();
    },
    toEditEngineer(row) {
      this.title = "编辑工程";
      this.form = _.clone(row);
      this.dialogFormVisible = true;
    },
    toEndEngineer(id) {
      this.$confirm("此操作将结束该工程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        get("/engineer/finish", { id }).then(() => {
          this.pageQueryEngineer();
          this.$message({
            type: "success",
            message: "结束成功!",
          });
        });
      });
    },
    toRemoveEngineer(id) {
      this.$confirm("此操作将删除该工程, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        del("/engineer/deleteById", { id }).then(() => {
          this.pageQueryEngineer();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        });
      });
    },
    toBindEngineer(row) {
      this.$router.push({
        path: "Details",
        query: row,
      });
    },
    //网球请求
    //分页查询设备信息
    pageQueryEngineer() {
      get("/engineer/pageQuery", this.params).then((res) => {
        this.resData = res.data;
      });
    },
    getCharge() {
      get("/user/pageQuery", {
        page: 1,
        pageSize: 100,
        role_id: 3,
      }).then((res) => {
        this.charges = res.data.list;
      });
    },
    getCustomer() {
      get("/user/pageQuery", {
        page: 1,
        pageSize: 100,
        role_id: 4,
      }).then((res) => {
        this.customer = res.data.list;
      });
    },
  },
};
</script>

<style>
</style>